package com.ssy.lingxi.contract.serviceimpl;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.contract.entity.ContractTextDO;
import com.ssy.lingxi.contract.model.vo.common.response.ContractTextVO;
import com.ssy.lingxi.contract.model.vo.manage.request.ContractTextUpdateVO;
import com.ssy.lingxi.contract.repository.ContractTextRepository;
import com.ssy.lingxi.contract.service.IContractTextService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 合同文本接口实现
 * @author 伍将
 * @date 2021/2/8
 * @version 2.0.0
 */
@Service
public class ContractTextServiceImpl implements IContractTextService {

    @Resource
    private ContractTextRepository repository;

    /**
     * 查询合同文本详情
     * @author 伍将
     * @date 2021/2/8
     * @param contractId:
     * @return 合同文本查询返回的
     **/
    @Override
    public Wrapper<ContractTextVO> getDetail(Long contractId) {

        ContractTextVO result = null;
        ContractTextDO entity = repository.findByContractId(contractId);
        if (null != entity) {
            result = new ContractTextVO();
            result.setId(entity.getId());
            result.setTemplateId(entity.getTemplateId());
            result.setIsUseElectronicContract(entity.getIsUseElectronicContract());
            result.setContractName(entity.getContractName());
            result.setContractUrl(entity.getContractUrl());
        }

        return Wrapper.success(result);
    }

    /**
     * 保存合同文本
     * @author 伍将
     * @date 2021/2/25
     * @param contractId: 合同id
     * @param textVO: 合同文本信息
     * @return 操作结果
     **/
    @Override
    public Wrapper<Void> save(Long contractId, ContractTextUpdateVO textVO) {
        //清空历史
        if(textVO.getId()==null||textVO.getId()==0){
            deleteByContractId(contractId);
            textVO.setId(null);
        }else{
            ContractTextDO contractTextDO = repository.findById(textVO.getId()).orElse(null);
            if(contractTextDO==null||!contractId.equals(contractTextDO.getContractId())){
                throw new BusinessException(ResponseCode.CONTRACT_GET_TEXT_ERROR,"合同文件不存在");
            }
        }
        //保存新的或者更新
        ContractTextDO contractText = new ContractTextDO();
        contractText.setId(textVO.getId());
        contractText.setContractId(contractId);
        contractText.setTemplateId(textVO.getTemplateId());
        contractText.setContractName(textVO.getContractName());
        contractText.setContractUrl(textVO.getContractUrl());
        contractText.setIsUseElectronicContract(textVO.getIsUseElectronicContract());
        repository.saveAndFlush(contractText);

        return Wrapper.success();
    }

    /**
     * 根据合同id删除合同文本
     * @author 伍将
     * @date 2021/3/3
     * @param contractId: 合同id
     * @return 操作结果
     **/
    @Override
    public Wrapper<Void> deleteByContractId(Long contractId){
        repository.deleteByContractId(contractId);

        return Wrapper.success();
    }

    @Override
    public void updateUrlById(Long contractId, String contractUrl) {
        ContractTextDO contractTextDO = repository.findByContractId(contractId);
        if(contractTextDO!=null){
            contractTextDO.setContractUrl(contractUrl);
            repository.saveAndFlush(contractTextDO);
        }
    }
}
